const GROUP_NAME = 'orders';
const {jwtHeaderDefine} = require('../utils/router-helper');
const Joi = require('joi');
const models = require('../models');

module.exports = [
  {
    method: 'POST',
    path: `/${GROUP_NAME}`,
    handler: async (request, reply) => {
        await models.sequelize.transaction(t => {//使用sequelize.transaction使用事务功能
            let result = models.orders.create({
              user_id: request.auth.credentials.userId
            },{
              transaction: t
            })
            .then(order => {
              let goodsList = [];
              request.payload.goodsList.forEach(item => {
                goodsList.push(models.order_goods.create({
                  order_id: order.dataValues.id,
                  goods_id: item.goods_id,
                  single_price: 4.9,
                  count: item.count,
                }))
              })
              return Promise.all(goodsList);
            })
            return result;
        })
        .then(() => {
          reply({
            code: '0',
            msg: 'success'
          })
        })
        .catch((err) => {
          reply({
            code: '0',
            msg: err
          })
        })
    },
    config: {
      tags: ['api', GROUP_NAME],
      description: '创建订单',
      validate: {
        payload: {
          goodsList: Joi.array().items(
            Joi.object().keys({
              goods_id: Joi.number().integer(),
              count: Joi.number().integer(),
            }),
          ),
        },
        ...jwtHeaderDefine,
      }
    },
  },
  {
    method: 'POST',
    path: `/${GROUP_NAME}/{orderId}/pay`,
    handler: async (request, reply) => {
      reply();
    },
    config: {
      tags: ['api', GROUP_NAME],
      description: '支付某条订单',
    },
  },
];